﻿using System;
using System.Globalization;
using Monkey.Warrenty.Biz;
using Monkey.Warrenty.EntityModel;

namespace MonkeyWarranty.Web.Products
{
    public partial class New : System.Web.UI.Page
    {
        #region Properties

        private static ProductBiz _productBiz;
        private static ProductBiz ProductManager
        {
            get { return _productBiz ?? (_productBiz = new ProductBiz()); }
        }

        #endregion

        #region Events

        protected void Page_Load(object sender, EventArgs e)
        {
            // Load data
            if (!IsPostBack)
            {
                // Load product groups
                var groups = ProductManager.GetAllProductGroups();
                groups.Insert(0, new ProductGroup { Id = 0, Name = string.Empty });
                ddlProductGroups.DataTextField = "Name";
                ddlProductGroups.DataValueField = "Id";
                ddlProductGroups.DataSource = groups;
                ddlProductGroups.DataBind();

                // load manufacturers
                var manufacturers = ProductManager.GetAllManufacturers();
                manufacturers.Insert(0, new Manufacturer { Id = 0, Name = string.Empty });
                ddlManufacturers.DataTextField = "Name";
                ddlManufacturers.DataValueField = "Id";
                ddlManufacturers.DataSource = manufacturers;
                ddlManufacturers.DataBind();

                // get auto code
                var nextCode = ProductManager.GetNextCode();
                txtCode.Text = string.Format("{0:00000}", nextCode);
                txtSortOrder.Text = nextCode.ToString(CultureInfo.InvariantCulture);
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            lblErrorMessage.Text = string.Empty;
            errorMessage.Visible = false;

            // validate inputs
            var code = txtCode.Text.Trim();
            if (code.Length == 0)
            {
                lblErrorMessage.Text = Resources.GlobalStrings.CodeIsRequired;
                errorMessage.Visible = true;
                return;
            }
            var name = txtName.Text.Trim();
            if (name.Length == 0)
            {
                lblErrorMessage.Text = Resources.GlobalStrings.NameIsRequired;
                errorMessage.Visible = true;
                return;
            }
            var measurement = txtMeasurement.Text.Trim();
            var descrition = txtDescription.Text;
            int sortOrder;
            int.TryParse(txtSortOrder.Text.Trim(), out sortOrder);
            var isHidden = chkHidden.Checked;
            var isWarranty = chkWarranty.Checked;
            var isAccessory = chkAccessory.Checked;
            int productGroupId;
            int.TryParse(ddlProductGroups.SelectedValue, out productGroupId);
            int manufcturerId;
            int.TryParse(ddlManufacturers.SelectedValue, out manufcturerId);

            // get data
            var newProduct = new Product
                                 {
                                     Code = code,
                                     Name = name,
                                     Description = descrition,
                                     SortOrder = sortOrder,
                                     Hidden = isHidden,
                                     IsWarranty = isWarranty,
                                     IsAccessory = isAccessory,
                                     Measurement = measurement
                                 };
            if (productGroupId > 0) newProduct.ProductGroupId = productGroupId;
            if (manufcturerId > 0) newProduct.ManufacturerId = manufcturerId;

            // save data
            ProductManager.CreateProduct(newProduct);

            // return
            Response.Redirect("List.aspx");
        }

        #endregion
    }
}